package com.example.idachuappone.index.dialog;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import antistatic.spinnerwheel.WheelView;
import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;

import com.example.idachuappone.R;
import com.example.idachuappone.index.welcomentity.Packages;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class DialogPackage {
	private Context context;
	private List<Packages> list;

	class AnonymousClass_1 implements OnClickListener {
		private final/* synthetic */CallBack val$callBack;
		private final/* synthetic */WheelView val$picker_packages;

		AnonymousClass_1(CallBack callBack, WheelView wheelView) {
			this.val$callBack = callBack;
			this.val$picker_packages = wheelView;
		}

		@Override
		public void onClick(View v) {
			this.val$callBack.btnOk(DialogPackage.this.list.get(this.val$picker_packages.getCurrentItem()));
		}
	}

	public static interface CallBack {
		void btnOk(Packages packages);
	}

	private class PackagesPickerAdapter extends AbstractWheelTextAdapter {
		protected PackagesPickerAdapter(Context context) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return DialogPackage.this.list == null ? 0 : DialogPackage.this.list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			if (DialogPackage.this.list == null) {
				return null;
			}
			if (Integer.parseInt(DialogPackage.this.list.get(index).getPrice()) != 0) {
				return DialogPackage.this.list != null ? new StringBuilder(String.valueOf(DialogPackage.this.list.get(
						index).getName())).append("/").append(DialogPackage.this.list.get(index).getPrice())
						.append("\u5143(").append(DialogPackage.this.list.get(index).getDescription()).append(")")
						.toString() : null;
			} else {
				return DialogPackage.this.list == null ? null : DialogPackage.this.list.get(index).getName();
			}
		}
	}

	public DialogPackage(List<Packages> list, Context context) {
		this.list = list;
		this.context = context;
	}

	public Dialog showPackageDialog(Activity activity, CallBack callBack) {
		View view = LayoutInflater.from(this.context).inflate(R.layout.dialog_packages, null);
		WheelView picker_packages = (WheelView) view.findViewById(R.id.picker_packages);
		picker_packages.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		picker_packages.setViewAdapter(new PackagesPickerAdapter(this.context));
		picker_packages.setCurrentItem(1);
		((TextView) view.findViewById(R.id.btn_packages_ok)).setOnClickListener(new AnonymousClass_1(callBack,
				picker_packages));
		view.setMinimumWidth((int) ((Utils.getScreenWidth(activity)) * 0.9d));
		Dialog dialog = new Dialog(this.context, 2131361801);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(17);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}
}
